<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\controller;

class AdminReseller extends \app\AdminRest
{
    protected $model;
    protected $user_model;
    protected $cash_model;
    protected $wallet_model;
    public function __construct(\think\App $app)
    {
        parent::__construct($app);
        $this->model = new \app\massage\model\DistributionList();
        $this->user_model = new \app\massage\model\User();
        $this->cash_model = new \app\massage\model\Commission();
        $this->wallet_model = new \app\massage\model\Wallet();
    }
    public function resellerList()
    {
        $input = $this->_param;
        $dis[] = ["a.uniacid", "=", $this->_uniacid];
        if(!empty($input["status"])) {
            $dis[] = ["a.status", "=", $input["status"]];
        } else {
            $dis[] = ["a.status", ">", -1];
        }
        if(!empty($input["reseller_id"])) {
            $dis[] = ["a.id", "<>", $input["reseller_id"]];
        }
        if($this->_user["is_admin"] == 0) {
            $admin_model = new \app\massage\model\Admin();
            $admin_arr = $admin_model->where("id", "in", $this->admin_arr)->where(["reseller_auth" => 1])->column("id");
            $dis[] = ["a.admin_id", "in", $admin_arr];
        }
        if(!empty($input["start_time"]) && !empty($input["end_time"])) {
            $start_time = $input["start_time"];
            $end_time = $input["end_time"];
            $dis[] = ["a.create_time", "between", $start_time . "," . $end_time];
        }
        $where = [];
        if(!empty($input["name"])) {
            $where[] = ["a.user_name", "like", "%" . $input["name"] . "%"];
            $where[] = ["a.mobile", "like", "%" . $input["name"] . "%"];
        }
        $data = $this->model->adminDataList($dis, $input["limit"], $where);
        $list = ["all", "ing", "pass", "4" => "nopass"];
        foreach ($list as $k => $value) {
            $dis_s = [];
            $dis_s[] = ["uniacid", "=", $this->_uniacid];
            if(!empty($k)) {
                $dis_s[] = ["status", "=", $k];
            } else {
                $dis_s[] = ["status", ">", -1];
            }
            if($this->_user["is_admin"] == 0) {
                $dis_s[] = ["admin_id", "=", $this->_user["admin_id"]];
            }
            $data[$value] = $this->model->where($dis_s)->count();
        }
        return $this->success($data);
    }
    public function resellerInfo()
    {
        $input = $this->_param;
        $dis = ["id" => $input["id"]];
        $info = $this->model->dataInfo($dis);
        $user_model = new \app\massage\model\User();
        $admin_model = new \app\massage\model\Admin();
        $info["nickName"] = $user_model->where(["id" => $info["user_id"]])->value("nickName");
        $info["admin_name"] = $admin_model->where(["id" => $info["admin_id"], "status" => 1])->value("agent_name");
        return $this->success($info);
    }
    public function resellerUpdate()
    {
        $input = $this->_input;
        $diss = ["id" => $input["id"]];
        $info = $this->model->dataInfo($diss);
        if(!empty($input["status"]) && in_array($input["status"], [2, 4, -1])) {
            $input["sh_time"] = time();
            if($input["status"] == -1) {
                $cash = $this->user_model->where(["id" => $info["user_id"]])->sum("new_cash");
                $this->user_model->where(["id" => $info["user_id"]])->update(["new_cash" => 0]);
                $this->user_model->where(["pid" => $info["user_id"]])->update(["pid" => 0]);
                $input["del_time"] = time();
                $input["del_cash"] = $cash;
            }
        }
        $data = $this->model->dataUpdate($diss, $input);
        if(isset($input["status"])) {
            $update = ["is_fx" => 0];
            if($input["status"] == 2) {
                $update["is_fx"] = 1;
            }
            $this->user_model->dataUpdate(["id" => $info["user_id"]], $update);
        }
        return $this->success($data);
    }
    public function partnerDataList()
    {
        $input = $this->_param;
        $dis[] = ["a.uniacid", "=", $this->_uniacid];
        if($this->_user["is_admin"] == 0) {
            $dis[] = ["b.admin_id", "=", $this->_user["admin_id"]];
        }
        if(getFxStatus($this->_uniacid) == 1) {
            $dis[] = ["b.status", "=", 2];
        }
        $where = [];
        if(!empty($input["name"])) {
            $where[] = ["a.nickName", "like", "%" . $input["name"] . "%"];
            $where[] = ["b.user_name", "like", "%" . $input["name"] . "%"];
            $where[] = ["b.mobile", "like", "%" . $input["name"] . "%"];
        }
        if(!empty($input["start_time"]) && !empty($input["end_time"])) {
            $dis[] = ["b.sh_time", "between", $input["start_time"] . "," . $input["end_time"]];
        }
        $data = $this->model->userDataList($dis, $where, $input["limit"]);
        if(!empty($data["data"])) {
            foreach ($data["data"] as &$v) {
                $del_time = $this->model->where(["user_id" => $v["user_id"], "status" => -1])->max("del_time");
                $fx_check = !empty($del_time) ? 1 : 0;
                $resller_data = $this->model->getResellerData($v["user_id"], $fx_check, $del_time, $v["id"]);
                $v = array_merge($v, $resller_data);
            }
        }
        return $this->success($data);
    }
    public function subUser()
    {
        $input = $this->_param;
        $dis[] = ["status", "=", 1];
        $dis[] = ["pid", "=", $input["user_id"]];
        if(!empty($input["name"])) {
            $dis[] = ["nickName", "like", "%" . $input["name"] . "%"];
        }
        $data = $this->user_model->where($dis)->field("id,nickName,avatarUrl,create_time,phone")->paginate($input["limit"])->toArray();
        return $this->success($data);
    }
    public function delSubUser()
    {
        $input = $this->_input;
        $res = $this->user_model->dataUpdate(["id" => $input["user_id"]], ["pid" => 0]);
        return $this->success($res);
    }
    public function subReseller()
    {
        $input = $this->_param;
        if(!empty($input["id"])) {
            $dis[] = ["pid", "=", $input["id"]];
        }
        if(!empty($input["user_id"])) {
            $cap_dis[] = ["user_id", "=", $input["user_id"]];
            $cap_dis[] = ["status", "in", [2, 3]];
            if($this->_user["is_admin"] == 0) {
                $cap_dis[] = ["admin_id", "=", $this->_user["admin_id"]];
            }
            $resller = $this->model->dataInfo($cap_dis);
            $resller_id = !empty($resller) ? $resller["id"] : -1;
            $dis[] = ["pid", "=", $resller_id];
        }
        $dis[] = ["status", "in", [2, 3]];
        if(!empty($input["name"])) {
            $dis[] = ["user_name", "like", "%" . $input["name"] . "%"];
        }
        $data = $this->model->dataList($dis, $input["limit"]);
        $config_model = new \app\massage\model\Config();
        $user_model = new \app\massage\model\User();
        if(!empty($data["data"])) {
            foreach ($data["data"] as &$v) {
                if($v["status"] == 2) {
                    $v["sh_time"] = !empty($v["sh_time"]) ? $v["sh_time"] : $v["create_time"];
                }
                $v["avatarUrl"] = $user_model->where(["id" => $v["user_id"]])->value("avatarUrl");
                $v["nickName"] = $user_model->where(["id" => $v["user_id"]])->value("nickName");
                $del_time = $this->model->where(["user_id" => $v["id"], "status" => -1])->max("del_time");
                $fx_check = !empty($del_time) ? 1 : 0;
                $resller_data = $this->model->getResellerData($v["user_id"], $fx_check, $del_time, $v["id"]);
                $v = array_merge($v, $resller_data);
                $v["balance"] = 0 <= $v["balance"] ? $v["balance"] : getConfigSetting($v["uniacid"], "user_agent_balance");
                $v["level_balance"] = 0 <= $v["level_balance"] ? $v["level_balance"] : getConfigSetting($v["uniacid"], "user_level_balance");
            }
        }
        return $this->success($data);
    }
    public function resellerRelationshipTop()
    {
        $input = $this->_param;
        $user_model = new \app\massage\model\User();
        if($this->_user["is_admin"] == 0) {
            $cap_dis[] = ["admin_id", "=", $this->_user["admin_id"]];
        }
        $cap_dis[] = ["user_id", "=", $input["user_id"]];
        $cap_dis[] = ["status", "in", [2, 3]];
        $resller = $this->model->dataInfo($cap_dis);
        if(!empty($resller)) {
            $resller["nickName"] = $user_model->where(["id" => $resller["user_id"]])->value("nickName");
            $resller["avatarUrl"] = $user_model->where(["id" => $resller["user_id"]])->value("avatarUrl");
        }
        $data["user_info"] = !empty($resller) ? $resller : [];
        if(!empty($resller)) {
            $dis[] = ["id", "=", $resller["pid"]];
            $dis[] = ["status", "in", [2, 3]];
            $level_resller = $this->model->dataInfo($dis);
            if(!empty($level_resller)) {
                $level_resller["nickName"] = $user_model->where(["id" => $level_resller["user_id"]])->value("nickName");
                $level_resller["avatarUrl"] = $user_model->where(["id" => $level_resller["user_id"]])->value("avatarUrl");
            }
            $data["level_info"] = $level_resller;
        } else {
            $data["level_info"] = [];
        }
        return $this->success($data);
    }
    public function resellerOrderList()
    {
        $input = $this->_param;
        $order_model = new \app\payreseller\model\Order();
        $user_model = new \app\massage\model\User();
        $comm_model = new \app\massage\model\Commission();
        $dis[] = ["pay_type", "=", 2];
        $dis[] = ["uniacid", "=", $this->_uniacid];
        if(!empty($input["order_code"])) {
            $dis[] = ["order_code", "like", "%" . $input["order_code"] . "%"];
        }
        if(!empty($input["start_time"])) {
            $dis[] = ["create_time", "between", $input["start_time"] . "," . $input["end_time"]];
        }
        $data = $order_model->dataList($dis, $input["limit"]);
        if(!empty($data["data"])) {
            foreach ($data["data"] as &$v) {
                $v["user_info"] = $user_model->where(["id" => $v["user_id"]])->field("nickName,avatarUrl")->find();
                if(!empty($v["top_reseller_id"])) {
                    $v["inv_user_name"] = $this->model->where(["id" => $v["top_reseller_id"]])->value("user_name");
                    $v["inv_comm_cash"] = $comm_model->where(["type" => 15, "order_id" => $v["id"], "status" => 2])->sum("cash");
                }
            }
        }
        return $this->success($data);
    }
    public function updateRecommendCash()
    {
        $input = $this->_input;
        $update = ["recommend_cash" => $input["recommend_cash"], "recommend_range" => $input["recommend_range"], "recommend_day" => $input["recommend_day"], "recommend_time" => time()];
        foreach ($input["id"] as $value) {
            $res = $this->model->dataUpdate(["id" => $value], $update);
        }
        return $this->success($res);
    }
    public function recommendRecord()
    {
        $input = $this->_param;
        $recommend_model = new \app\massage\model\ResellerRecommendCash();
        $user_model = new \app\massage\model\User();
        $dis[] = ["a.uniacid", "=", $this->_uniacid];
        if(!empty($input["reseller_name"])) {
            $dis[] = ["b.user_name", "like", "%" . $input["reseller_name"] . "%"];
        }
        if(!empty($input["start_time"])) {
            $dis[] = ["a.create_time", "between", $input["start_time"] . "," . $input["end_time"]];
        }
        if($this->_user["is_admin"] == 0) {
            $dis[] = ["b.admin_id", "=", $this->_user["admin_id"]];
        }
        $data = $recommend_model->alias("a")->join("massage_distribution_list b", "a.reseller_id = b.id")->where($dis)->field("a.*,b.user_name as reseller_name")->group("a.id")->order("a.id desc")->paginate($input["limit"])->toArray();
        if(!empty($data["data"])) {
            foreach ($data["data"] as &$v) {
                $v["user_info"] = $user_model->where(["id" => $v["user_id"]])->field("nickName,avatarUrl")->find();
            }
        }
        return $this->success($data);
    }
}

?>